<?php

namespace app\common\model\system;

use cores\BaseModel;
use think\model\relation\HasOne;

class Department extends BaseModel
{
    protected $name = 'department';

    protected $pk = 'department_id';

    protected $append = ['children'];

    public function getChildrenAttr($value, $data)
    {
        return $this->where([
            'parent_department_id' => $data['department_id'],
            'is_delete' => 0,
        ])
            ->with(['admin_user', 'responsible_admin_user'])
            ->select();
    }

    /**
     * 详情信息
     * @param int $departmentId
     * @return array|null|static
     */
    public static function detail(int $departmentId)
    {
        return self::get($departmentId);
    }

    /**
     * 创建人信息
     * @return HasOne
     */
    public function adminUser()
    {
        return $this->hasOne('app\common\model\admin\User', 'admin_user_id', 'admin_user_id');
    }

    /**
     * 负责人信息
     * @return HasOne
     */
    public function responsibleAdminUser()
    {
        return $this->hasOne('app\common\model\admin\User', 'admin_user_id', 'responsible_admin_user_id');
    }

}
